﻿<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Routes extends CI_Controller{

	private static $url = 'http://houtai.meicii.com/isdk/';
        private static $key = "M1AA1UOTTQPW";


        public function __construct(){
		
		parent::__construct();
		
		$this->load->model('comm_model');
		
		if(!$this->session->userdata('currency')){
			
			$currency = $this->comm_model->get_currency();
			
			$this->session->set_userdata('currency',$currency);
			
		}
		
	}
	
	public function home(){
		
		if($userinfo = $this->session->userdata('userinfo')){
				
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
			
		}
		
		if($all_cart_products = $this->session->userdata('product')){
			
			$data['pro_item'] = count($all_cart_products);
			
		}else{
			
			$data['pro_item'] = 0;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		$data['proleft'] = $this->comm_model->get_limit_product(25,4);
		$data['proright'] = $this->comm_model->get_limit_product(125,3);
		
		$currency = $this->session->userdata('currency');
		
		$data['product_info']['product_price'] *= $currency->rate;
			
		$data['product_info']['product_oprice'] *= $currency->rate;
		
		foreach ($data['proleft'] as $key=>$val){
		
			$data['proleft'][$key]['product_price'] = ceil($data['proleft'][$key]['product_price']*$currency->rate*100)/100;
		
			$data['proleft'][$key]['product_oprice'] = ceil($data['proleft'][$key]['product_oprice']*$currency->rate*100)/100;
		}
		
		foreach ($data['proright'] as $key=>$val){
		
			$data['proright'][$key]['product_price'] = ceil($data['proright'][$key]['product_price']*$currency->rate*100)/100;
		
			$data['proright'][$key]['product_oprice'] = ceil($data['proright'][$key]['product_oprice']*$currency->rate*100)/100;
		}
		
		$data['symbol'] = $currency->symbol;
		
		$site_info = $this->comm_model->get_site_info();

		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
		
		$this->load->view('header',$data);
		$this->load->view('home');
		$this->load_footer();
	}
	
	public function cate($cate_p = '',$cate_c = '',$page = 1){
		
		if(!is_numeric($page)){
			
			show_404();exit;
		}
		
		$all_cate = $this->comm_model->get_all_cate();
		
		$isex = 0;
		
		foreach ($all_cate as $k=>$v){
			
			if($cate_c == $v['cateurl']){
				
				$isex = 1;
				
				break;
			}
		}
		
		if($isex==0&&!is_numeric($cate_c)){
			
			show_404();exit;
			
		}
		
		
		if($all_cart_products = $this->session->userdata('product')){
				
			$data['pro_item'] = count($all_cart_products);
				
		}else{
				
			$data['pro_item'] = 0;
		}
		
		if($userinfo = $this->session->userdata('userinfo')){
				
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
			
		}
		
		$cate_name_arr = $this->comm_model->get_main_cate_name();
		
		if($this->comm_model->iscates($cate_p)==0){
			
			header("location:/product/$cate_p");
			
			exit;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		$current_cate_p = $this->comm_model->get_cates($cate_p);//获取当前主分类信息
		
		if($isex == 1){
		
			$cateinfo = $this->comm_model->get_cates($cate_c);
			$data['title'] = $cateinfo['title'];
			$data['keywords'] = $cateinfo['keyword'];
			$data['description'] = $cateinfo['discription'];
		
		}else{
		
			$data['title'] = $current_cate_p['title'];
			$data['keywords'] = $current_cate_p['keyword'];
			$data['description'] = $current_cate_p['discription'];
		}
		
		$data['current_cate_p'] = $current_cate_p['id'];
		
		$cate_p_arr = array(
				'catename' => str_replace('-', ' ', $cate_p),
				'cateurl' => $cate_p
		);
		
		$data['cate_p'] = $cate_p_arr;
		
		$total_pages = 0;
		
		if(is_numeric($cate_c)){
			
			$data['curr_page'] = $cate_c;
			
			$data['cate_c'] = $cate_p_arr;
			
			$cate_pages = array(
					'page' => $cate_c-1,
					'page_num' => 24
			);
			
			$cate_url = $cate_p;
			
		}else{
			
			$data['curr_page'] = $page;
			
			$cate_c_arr = array(
					'catename' => str_replace('-', ' ', $cate_c),
					'cateurl' => $cate_c
			);
			
			$data['cate_c'] = $cate_c_arr;
			
			$cate_pages = array(
					'page' => $page-1,
					'page_num' => 24
			);
			
			$cate_url = $cate_c;
		}
		
		$data['product_list'] = $this->comm_model->get_cate_product($cate_url,$cate_pages,$total_pages);
		
		$currency = $this->session->userdata('currency');
		
		foreach ($data['product_list'] as $key=>$val){
			
			$data['product_list'][$key]['product_price'] *= $currency->rate;
			
			$data['product_list'][$key]['product_oprice'] *= $currency->rate;
		}
		
		$data['symbol'] = $currency->symbol;
		
		$data['total_pages'] = $total_pages;
		
		$data['childcates'] = $this->comm_model->get_childcates($cate_p);
		
		//var_dump($data);exit;
		
		$this->load->view('header',$data);
		$this->load->view('category');
		$this->load_footer();
	}
	
	public function product($product_url = ''){
		
		//$this->output->cache(5000000);
		
		if($userinfo = $this->session->userdata('userinfo')){
				
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
			
		}
		
		if($all_cart_products = $this->session->userdata('product')){
				
			$data['pro_item'] = count($all_cart_products);
				
		}else{
				
			$data['pro_item'] = 0;
		}

		$data['categorys'] = $this->comm_model->get_category();
		
		$res = $this->comm_model->get_pre_cate($product_url);
		
		$data['cate_c'] = $res['cate_c_arr'];
		
		$data['cate_p'] = $res['cate_p_arr'];
		
		$data['current_cate_p'] = $res['cate_p_arr']['id']; //主分类ID
		
		$data['product_info'] = $this->comm_model->get_pro_info($product_url);
		
		$data['title'] = $data['product_info']['title'];
		$data['keywords'] = $data['product_info']['keyword'];
		$data['description'] = $data['product_info']['discription'];
		
		$img_arr = $this->comm_model->get_pic($data['product_info']['product_images'],$data['product_info']['product_id']);
		
		$data['product_info']['product_images'] = $img_arr;

		$data['more_products'] = $this->comm_model->get_limit_product(0,4,$res['cate_p_arr']['id']);
		
		$currency = $this->session->userdata('currency');
				
		$data['product_info']['product_price'] = ceil($data['product_info']['product_price']*$currency->rate*100)/100;
			
		$data['product_info']['product_oprice'] = ceil($data['product_info']['product_oprice']*$currency->rate*100)/100;
	
		foreach ($data['more_products'] as $key=>$val){
		
			$data['more_products'][$key]['product_price'] *= $currency->rate;
		
			$data['more_products'][$key]['product_oprice'] *= $currency->rate;
		}
		
		$data['symbol'] = $currency->symbol;


		$this->load->view('product',$data);
		$this->load_footer();

	}
	
	public function sign_l(){
		
		if(array_key_exists('userinfo',$this->session->all_userdata())){
				
			header('location:http://'.$_SERVER['HTTP_HOST']);exit;
		}

		if($loginerror = array_key_exists('loginerror',$this->session->all_userdata())?$this->session->userdata('loginerror'):false){
		
			$data['loginerror'] = $loginerror;
		
			$this->session->unset_userdata('loginerror');
		
		}else{
		
			$data['loginerror'] = '';
		}
		
		$data['site_url'] = $_SERVER['SERVER_NAME'];
		
		$data['countrys'] = $this->comm_model->get_countrys();
		
		$data['site_info'] = $this->comm_model->get_site_info();
		
		$data['title'] = $data['site_info']['title'];
		$data['keywords'] = $data['site_info']['keyword'];
		$data['description'] = $data['site_info']['discription'];
		
		$data['re_url'] = '';
		
		if($all_cart_products = $this->session->userdata('product')){
				
			$data['pro_item'] = count($all_cart_products);
				
		}else{
				
			$data['pro_item'] = 0;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
	//	print_r($data);exit;
		
		$this->load->view('header',$data);
		$this->load->view('sign_l');
		$this->load_footer();
	}
	
	public function sign_r(){
		
		if(array_key_exists('userinfo',$this->session->all_userdata())){
			
			header('location:http://'.$_SERVER['HTTP_HOST']);exit;
		}
	
		if($registerror = array_key_exists('registerror',$this->session->all_userdata())?$this->session->userdata('registerror'):false){
	  			
	  		$data['registerror'] = $registerror;
	  			
	  		$this->session->unset_userdata('registerror');
	  			
	  	}else{
	  		
	  		$data['registerror'] = '';
	  	}
	
		$data['site_url'] = $_SERVER['SERVER_NAME'];
	
		$data['countrys'] = $this->comm_model->get_countrys();
	
		$data['site_info'] = $this->comm_model->get_site_info();
		
		$data['title'] = $data['site_info']['title'];
		$data['keywords'] = $data['site_info']['keyword'];
		$data['description'] = $data['site_info']['discription'];
	
		$data['re_url'] = '';
	
		if($all_cart_products = $this->session->userdata('product')){
	
			$data['pro_item'] = count($all_cart_products);
	
		}else{
	
			$data['pro_item'] = 0;
		}
	
		$data['categorys'] = $this->comm_model->get_category();
	
		//	print_r($data);exit;
	
		$this->load->view('header',$data);
		$this->load->view('sign_r');
		$this->load_footer();
	}
	
	public function register(){
		
		$data = $registinfo = $this->input->post();		 
		
		foreach ($registinfo as $k=>$v){
		
			if(empty($v)){
					
				$this->session->set_userdata('registerror',$k.'is empty!');
					
				header("location:".$_SERVER['HTTP_REFERER']);exit;
					
			}
		}
		
		if (!ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+",$registinfo['email'])){
		
		$this->session->set_userdata('registerror','Your mailbox is not in the correct format!');
					
		header("location:".$_SERVER['HTTP_REFERER']);exit;
		}
			
		if($registinfo['password'] == $registinfo['confirmpassword']){
		
			unset($registinfo['confirmpassword']);
		
			$registinfo['password'] = $this->encrypt->encode(json_encode($registinfo['password']),$get_key);
		
		}else{
		
		$this->session->set_userdata('registerror','Not the same password twice!');
		
			header("location:".$_SERVER['HTTP_REFERER']);exit;
		}
		
		$register_ip = $this->comm_model->get_onlineip();
		
		$site_info = $this->comm_model->get_site_info();
		
		$site_id = $site_info['siteid'];
		
		$shipaddr = array(
		
				'firstname' => 	$data['firstname'],
				'lastname' => $data['lastname'],
				'address' => $data['address'],
				'city' => $data['city'],
				'state' => $data['state'],
				'postcode' => $data['postcode'],
				'country' => $data['country'],
				'phone' => $data['tel']
		
		);
		$billaddr = array(
		
				'firstname' => 	$data['firstname'],
				'lastname' => $data['lastname'],
				'address' => $data['address'],
				'city' => $data['city'],
				'state' => $data['state'],
				'postcode' => $data['postcode'],
				'country' => $data['country'],
				'phone' => $data['tel']
		
		);
		
		$register = array(
		
				'email' => $data['email'],
				'password' => md5($data['password']),
				'firstname' => $data['firstname'],
				'lastname' => $data['lastname'],
				'ip' => $register_ip,
				'siteid' => $site_id,
				'shipaddr' => serialize($shipaddr),
				'billaddr' => serialize($billaddr)
		
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($register),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$site_id;
		//var_dump($register);die(0);
		$res = $this->comm_model->vpost(self::$url."register_user",$send_info);

		//echo $res;die(0);
		if($res){
			
			$result = json_decode($res,true);
			
			//var_dump($result);exit;
			
			foreach ($result as $key=>$val){
				
				$re[$key] = $val;
			}

			switch ($re['ack']){
				
				case 0:
												
					$this->session->set_userdata('userinfo',$re);
					
					echo "<script>alert('register success!');</script>";
						
					if($re_url = $this->input->post('re_url')){
							
						echo "<script>location.href='".$re_url."'</script>";
							
						exit;
							
					}else{
							
						echo "<script>location.href='".$_SERVER['HTTP_REFERER']."'</script>";
							
						exit;
							
					}
					
					break;
					
				case -1:				
					
					echo "<script>alert('Email already exists');</script>";
					
					if($re_url = $this->input->post('re_url')){
							
						echo "<script>location.href='".$re_url."'</script>";
					
						exit;
					
					}else{
					
						echo "<script>location.href='".$_SERVER['HTTP_REFERER']."'</script>";
					
						exit;
					
					}
					
					break;
					
				case -2:
					
					echo "<script>alert('regist fail');</script>";
					
					if($re_url = $this->input->post('re_url')){
					
						echo "<script>location.href='".$re_url."'</script>"; 
						
						exit;
						
					}else{
						
						echo "<script>location.href='".$_SERVER['HTTP_REFERER']."'</script>";
						
						exit;
								
					}
					
					break;
			}
			
		}
		
	}
	
	public function login(){
		
		$postdata = $this->input->post();
		
		if (empty($postdata['email'])){
			 
			$this->session->set_userdata('loginerror','email is empty!');
			 
			header("location:".$_SERVER['HTTP_REFERER']);exit;
			 
		}else if(empty($postdata['password'])){
		
			$this->session->set_userdata('loginerror','password is empty!');
		
			header("location:".$_SERVER['HTTP_REFERER']);exit;
			 
		}
		
		$register_ip = $this->comm_model->get_onlineip();
		
		$site_info = $this->comm_model->get_site_info();
		
		$site_id = $site_info['siteid'];
		
		$data = $this->input->post();
		
		$register_info = array(
				
				'siteid' => $site_id,
				'email' => $data['email'],
				'password' => md5($data['password'])
				
				
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($register_info),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$site_id;

		$res = $this->comm_model->vpost(self::$url."user_login",$send_info);
		
		if($res){
			
			$result = json_decode($res,true);
			
			switch ($result['ack']){
				
				case 0:
					
					$this->session->set_userdata('userinfo',$result);
						
					break;
					
				case -1:
					
					
					$this->session->set_userdata('loginerror','email or password error!');
			//		echo "<script>alert('email or password error!');</script>";
					
					break;
			}
				
			if($re_url = $this->input->post('re_url')){
					
				echo "<script>location.href='".$re_url."'</script>";
			
				exit;
			
			}else{
			
				echo "<script>location.href='".$_SERVER['HTTP_REFERER']."'</script>";
			
				exit;
			
                }
                
                  }
		
	}
	
	public function add_to_cart(){
		
		if($userinfo = $this->session->userdata('userinfo')){
				
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
			
			$uid= $userinfo['mid'];
			
		}else{
			
			$uid = false;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		if($this->input->post()){
			
			$this->comm_model->insert_cart();
		}	
		
		if($all_cart_products = $this->session->userdata('product')){
		
			$data['pro_item'] = count($all_cart_products);
		
		}else{
		
			$data['pro_item'] = 0;
		}
		
		$product_cart = $this->session->all_userdata();

		//转到购物车页面
		
		$data['cart_info'] = isset($product_cart['product'])?$product_cart['product']:'';
		
		
		//print_r($data['cart_info']);exit;
		
		$site_info = $this->comm_model->get_site_info();
		
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
		
		$site_id = $site_info['siteid'];
		
		$get_ship = array(
					
				'siteid' => $site_id
		
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($get_ship),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$site_id;
			
		$ships = $this->comm_model->vpost(self::$url."shiplist",$send_info);
		
		if($ships){
				
			$ship_result = json_decode($ships,true);
				
			foreach ($ship_result as $key=>$val){
		
				$re_ship[$key] = $val;
			}
				
			$data['ships'] = $re_ship;
				
		}else{
				
			$data['ships'] = '';
		}
		
		$data['currency'] = $product_cart['currency'];
		
		$data['symbol'] = $product_cart['currency']->symbol;
		
		//print_r($data['cart_info']);exit;
		
		$this->load->view('header',$data);
		$this->load->view('cart');
		$this->load_footer();
		
	}
	
	public function shipaddress(){
	
		if($userinfo = $this->session->userdata('userinfo')){
		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
				
			$uid= $userinfo['mid'];
				
		}else{
				
			$uid = false;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		$postship = $this->input->post('shippname');

		$site_info = $this->comm_model->get_site_info();
		
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
		
		$site_id = $site_info['siteid'];
		
		$get_ship = array(
					
				'siteid' => $site_id
		
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($get_ship),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$site_id;
			
		$ships = $this->comm_model->vpost(self::$url."shiplist",$send_info);
		
		if($ships){
				
			$ship_result = json_decode($ships,true);
				
			foreach ($ship_result as $k=>$v){
				
				if($v['shippname'] == $postship){
					
					$this->session->set_userdata('ships',$v);
				}
			}
				
		}
		
		if($all_cart_products = $this->session->userdata('product')){
		
			$data['pro_item'] = count($all_cart_products);
		
		}else{
		
			$data['pro_item'] = 0;
		}
		
		$data['countrys'] = $this->comm_model->get_countrys();
		
		$product_cart = $this->session->all_userdata();
		
		if($uid){
				
			$get_addr = array(
						
					'siteid' => $site_id,
					'uid' => $uid
			
			);
			
			$get_key = $this->get_key();
			
			$the_data = $this->encrypt->encode(json_encode($get_addr),$get_key);
			
			$send_info ='the_data='.$the_data .'&siteid='.$site_id;
				
			$res = $this->comm_model->vpost(self::$url."get_address",$send_info);
				
			if($res){
			
				$result = json_decode($res,true);
			
				foreach ($result as $key=>$val){
			
					$re[$key] = $val;
				}
			
				$data['shipaddr'] = unserialize($re['shipaddr']);
			
				$data['billaddr'] = unserialize($re['billaddr']);
			
				$data['shipaddr']['email'] = $re['email'];
			
			}
			
			//转到支付页面
				
			$data['cart_info'] = $product_cart['product'];

				
			$this->load->view('header',$data);
			$this->load->view('shipaddress');
			$this->load_footer();
			
		}else{
			
			header("location:/login_check");
			
			exit;
			
		}
				
		
	}
	
	public function login_check(){
		
		if($this->session->userdata('userinfo')){
				
			header("location: /");
				
			exit;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		if($all_cart_products = $this->session->userdata('product')){
		
			$data['pro_item'] = count($all_cart_products);
		
		}else{
		
			$data['pro_item'] = 0;
		}
		
		$data['site_url'] = $_SERVER['SERVER_NAME'];
		
		$data['re_url'] = '/shipaddress';
		
		$data['countrys'] = $this->comm_model->get_countrys();
		
		$data['site_info'] = $this->comm_model->get_site_info();
		
		$data['title'] = $data['site_info']['title'];
		$data['keywords'] = $data['site_info']['keyword'];
		$data['description'] = $data['site_info']['discription'];
		
		$data['categorys'] = $this->comm_model->get_category();
		
		$this->load->view('header',$data);
		$this->load->view('cartregister');
		$this->load_footer();
	
	}
	
	public function payment(){
		
		if($userinfo = $this->session->userdata('userinfo')){		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];				
			$uid= $userinfo['mid'];				
		}else{				
			$uid = false;
		}		
		$site_info = $this->comm_model->get_site_info();
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];			
		$post_addr = $this->input->post();		
		$this->session->set_userdata('shipaddress',$post_addr);		

		$ses_info = $this->session->all_userdata();
		if(array_key_exists('product',$ses_info)){
			$data['pro_item'] = count($ses_info['product']);
		}else{
			$data['pro_item'] = 0;
		}
		$data['cart_info'] = $ses_info['product'];	
		$data['currency'] = $ses_info['currency'];	
		$data['symbol'] = $ses_info['currency']->symbol;
		$subtotal = 0;		
		foreach ($ses_info['product'] as $k=>$v){			
			$subtotal += $v['qty']*$v['product_price'];
		}					
		$subtotal = ceil($subtotal*100)/100;
		if($subtotal >18000000){
			$this->session->set_userdata('useshipfee',0);
			$data['shippingfee'] = 0;
		}else{
			$this->session->set_userdata('useshipfee',1);
			$data['shippingfee'] = $ses_info['ships']['shippfee']*$ses_info['currency']->rate;
		}
		
		$this->load->view('header',$data);
		$this->load->view('payment');
		$this->load_footer();
				
	}
        
	public function refresh_pay(){
            $data = $this->session->userdata("dataaa");
            $this->load->view("refresh",$data);
        }
        
        public function do_pay(){
		
		if($userinfo = $this->session->userdata('userinfo')){
				
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
			
			$uid= $userinfo['mid'];
			
			$email = $userinfo['email'];
			
		}
		
		$add_post = $this->input->post();
		
		$product_cart = $this->session->all_userdata();
		
		$data['site_info'] = $this->comm_model->get_site_info();
		
		$shipaddr = $billaddr = $product_cart['shipaddress'];
		$shipfee = $product_cart['useshipfee'] == 0?0:$product_cart['ships']['shippfee']*($product_cart['currency']->rate);
		$post_data = array(
				'uid' => $uid,
				'email' => $email,
				'shiptype' => $product_cart['ships']['shippname'],
				'shippingfee' => $shipfee,
				'currency' => $product_cart['currency']->currency,
				'siteid' => $data['site_info']['siteid'],
				'message' => '',
				'shipaddr' => serialize($shipaddr),
				'billaddr' => serialize($billaddr),
				'status' => 'unpaid',
                                'ordersn'=>date("YmdHis"). str_pad(rand(0, 999),3,0)
		);
		
		$post_data['total'] = 0;
	
		foreach ($product_cart['product'] as $key => $val){
		
			$post_data['total'] += $val['qty']*$val['product_price']*($product_cart['currency']->rate);
		
			$post_data['goodslist'][] = array(
		
					'product_id' => $val['product_id'],
					'sku' => $val['sku'],
					'product_name' => $val['product_name'],
					'price' => $val['product_price']*($product_cart['currency']->rate),
					'qty' => $val['qty'],
					'attr' => $val['size'],
					'img' => $val['img'],
					'product_url' => $val['url']
			);
		}
		$post_data['total'] = $post_data['total']+$shipfee;
		$get_key = $this->get_key();
                
                //var_dump($post_data);die(0);
		
		$the_data = $this->encrypt->encode(json_encode($post_data),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$data['site_info']['siteid'];
		
		$res = $this->comm_model->vpost(self::$url."add_order/",$send_info);
			
		if($res){
			
			$result = json_decode($res,true);
				
			foreach ($result as $key=>$val){
					
				$re[$key] = $val;
			}
			
			if($re['ack']==0){
				
			if(array_key_exists('token', $product_cart)){
			
				$spreadinfo  = array(
						
						'token' => $product_cart['token'],
						'orderid' => $re['orderid']
				);
				
				$the_data = $this->encrypt->encode(json_encode($spreadinfo),$get_key);
				
				$send_info ='the_data='.$the_data .'&siteid='.$data['site_info']['siteid'];
				
				$res = $this->comm_model->vpost(self::$url."add_spread_order",$send_info);

			}
                                    $merchantno = '361100';
                                    /* MD5私钥 */
                                    $MD5key     = self::$key;
                                    /* 订单号 */
                                    $BillNo     = $post_data['ordersn'];
                                    /* 订单金额 */
                                    $Amount     = $post_data['total'];
                                    /* 速汇通开通币种
        对应的币种有   2:欧元  1:美元  6:日元  4:英镑  5:港币   7:澳元  11:加元  8:挪威克朗 3:人民币  12:丹麦克朗  13:瑞典克朗
    */
                                    $CurrencyArray  = array('EUR'=>'2', 'USD'=>'USD', 'JPY'=>'6', 'GBP'=>'4', 'AUD'=>'7', 'CAD'=>'11', 'NOK'=>'8','RMB'=>'3','DKK'=>'12','HKD'=>'5','SEK'=>'13');
                                    /* 支付币种 */
                                    $Currency   = $CurrencyArray['USD'];
                                    /* 支付币种符号 */
                                    $CurrencyName   = $_POST['CurrencyName'];
                                    
                                    /* 返回地址[必填]返回数据给商户，商户自己填写，需要在商户后台绑定 */
                                    $ReturnURL      = 'http://'.$_SERVER['HTTP_HOST'].'/return_url';
                                    /* 通知地址；为确保数据的正常返回  暂时不可用， 可不填写*/
                                    $NoticeURL      = 'http://'.$_SERVER['HTTP_HOST'].'/return_page';
                                    /* 备注信息[选填]填写网站的域名，方便以后维护,可不做修改 */
                                    $Remark         = 'http://'.$_SERVER['HTTP_HOST'];
                                    $Language = "en";
                                    
                                    $MD5src         =   $BillNo . "487570085"  . $Currency  . $post_data['total'].$MD5key;
                                    $MD5info        = md5($MD5src);
                                    
                                    
                                    $send_data = array(
                                                'product_name[0]' => $product_cart['product'][0]["product_name"],
                                                "merchantno"=>$merchantno,
                                                "siteid"=>"487570085",
                                                "order_sn"=>$BillNo,
                                                "iver"=>"3.14",
                                                "version"=>"zencart1.3.9",
                                                "embed"=>"Y",
                                                "rpcookie"=>"rpma9pjml53l6blkmu64281enh04",
                                                "order_time"=>date("Y-m-d H:i:s",time()),
                                                "language"=>$Language,
                                                "currency"=>$Currency,
                                                "amount"=>$post_data['total'],
                                                "shippingfee"=>$shipfee,
                                                "vat"=>"0.00",
                                                "discount"=>"0.00",
                                                "product_no[0]"=>"",
                                                "price_unit[0]"=>$post_data['total'],
                                                "quantity[0]"=>"1",
                                                "returnurl"=>$ReturnURL,
                                                "notifyurl"=>$NoticeURL,
                                                "verifycode"=>$MD5info,
						'shipfirstname'=>$product_cart['shipaddress']['firstname'],
						'shiplastname'=>$product_cart['shipaddress']['lastname'],
						'shipaddress'=>$product_cart['shipaddress']['address'],
						'shipcity'=>$product_cart['shipaddress']['city'],
						'shipcountry'=>$product_cart['shipaddress']['country'],
						'shipstate'=>$product_cart['shipaddress']['state'],
						'shippostcode'=>$product_cart['shipaddress']['postcode'],
                                                'email'=>$product_cart['userinfo']['email'],
						'shipphone'=>$product_cart['shipaddress']['phone'],
                                                'billfirstname'=>$product_cart['shipaddress']['firstname'],
						'billlastname'=>$product_cart['shipaddress']['lastname'],
						'billaddress'=>$product_cart['shipaddress']['address'],
						'billcity'=>$product_cart['shipaddress']['city'],
						'billcountry'=>$product_cart['shipaddress']['country'],
						'billstate'=>$product_cart['shipaddress']['state'],
						'billpostcode'=>$product_cart['shipaddress']['postcode'],
						'billphone'=>$product_cart['shipaddress']['phone']
						
				);
				
				$this->session->unset_userdata('product');
						
				//$dataaa['posti'] = $this->comm_model->do_pay($send_data);
                                $dataaa['posti'] = $send_data;
                              
                                $this->session->set_userdata('dataaa',$dataaa);
				$this->load->view('posti',$dataaa);
				
			}else{
				
				echo "pay fial";
			}
			
			}else{
				
			echo "order fail";
		}
	}
	
        public function continue_pay($orderid){
            $data['site_info'] = $this->comm_model->get_site_info();
		
		$data['title'] = $data['site_info']['title'];
		$data['keywords'] = $data['site_info']['keyword'];
		$data['description'] = $data['site_info']['discription'];
		
		$site_id = $data['site_info']['siteid'];
            $showproduct = array(
				
						'orderid' => $orderid
				
				);
				
				$get_key = $this->get_key();
				
				$the_data = $this->encrypt->encode(json_encode($showproduct),$get_key);
				
				$send_info ='the_data='.$the_data .'&siteid='.$site_id;
					
				$res = $this->comm_model->vpost(self::$url."product_list",$send_info);
				
				if($res){
                                       // echo $res;
					$result = json_decode($res,true);
				
					foreach ($result as $key=>$val){
							
						$re[$key] = $val;
					}

					$ses_info = $this->session->all_userdata();
					
					$currinfo = array();
					
					foreach ($ses_info['order_info']['order'] as $k=>$v){
						
						if($v['orderid'] == $orderid){
							
							$currinfo = $this->comm_model->get_currency($v['currency']);
						
						}
						
					}
					
					foreach ($re as $k=>$v){
						
						$re[$k]['symbol'] = $currinfo->symbol;
						
						$re[$k]['price'] =$re[$k]['price'];
						
					}
					
					$data['products'] = $re;
					
					foreach ($ses_info['order_info']['order'] as $key=>$val){
						
							if($val['orderid'] == $orderid){
								
								$order_info = $val;
								
								break;
								
							}
							
					}
						
					$order_info['shipaddr'] = unserialize($order_info['shipaddr']);
					
					$order_info['billaddr'] = unserialize($order_info['billaddr']);
					
					$data['order_info'] = $order_info;
                                        //var_dump($data);die(0);
				}
                                $merchantno = '361100';
                                    /* MD5私钥 */
                                    $MD5key     = self::$key;
                                    /* 订单号 */
                                    $BillNo     = $order_info['ordersn'];
                                    /* 订单金额 */
                                    $Amount     = $order_info['total'];
                                    /* 速汇通开通币种
        对应的币种有   2:欧元  1:美元  6:日元  4:英镑  5:港币   7:澳元  11:加元  8:挪威克朗 3:人民币  12:丹麦克朗  13:瑞典克朗
    */
                                    $CurrencyArray  = array('EUR'=>'2', 'USD'=>'USD', 'JPY'=>'6', 'GBP'=>'4', 'AUD'=>'7', 'CAD'=>'11', 'NOK'=>'8','RMB'=>'3','DKK'=>'12','HKD'=>'5','SEK'=>'13');
                                    /* 支付币种 */
                                    $Currency   = $order_info['currency'];
                                    /* 支付币种符号 */
                                    $CurrencyName   = $order_info['currency'];
                                    
                                    /* 返回地址[必填]返回数据给商户，商户自己填写，需要在商户后台绑定 */
                                    $ReturnURL      = 'http://'.$_SERVER['HTTP_HOST'].'/return_url';
                                    /* 通知地址；为确保数据的正常返回  暂时不可用， 可不填写*/
                                    $NoticeURL      = 'http://'.$_SERVER['HTTP_HOST'].'/return_page';
                                    /* 备注信息[选填]填写网站的域名，方便以后维护,可不做修改 */
                                    $Remark         = 'http://'.$_SERVER['HTTP_HOST'];
                                    $Language = "en";
                                    
                                    $MD5src         =   $BillNo . "487570085"  . $Currency  . $order_info['total'].$MD5key;
                                    $MD5info        = md5($MD5src);
                                $send_data = array(
                                                'product_name[0]' => $re[0]["product_name"],
                                                "merchantno"=>$merchantno,
                                                "siteid"=>"487570085",
                                                "order_sn"=>$BillNo,
                                                "iver"=>"3.14",
                                                "version"=>"zencart1.3.9",
                                                "embed"=>"Y",
                                                "rpcookie"=>"rpma9pjml53l6blkmu64281enh04",
                                                "order_time"=>date("Y-m-d H:i:s",time()),
                                                "language"=>"en",
                                                "currency"=>$Currency,
                                                "amount"=>$order_info['total'],
                                                "shippingfee"=>$order_info["shippingfee"],
                                                "vat"=>"0.00",
                                                "discount"=>"0.00",
                                                "product_no[0]"=>"",
                                                "price_unit[0]"=>$order_info['total'],
                                                "quantity[0]"=>"1",
                                                "returnurl"=>$ReturnURL,
                                                "notifyurl"=>$NoticeURL,
                                                "verifycode"=>$MD5info,
						'shipfirstname'=>$order_info['shipaddr']['firstname'],
						'shiplastname'=>$order_info['shipaddr']['lastname'],
						'shipaddress'=>$order_info['shipaddr']['address'],
						'shipcity'=>$order_info['shipaddr']['city'],
						'shipcountry'=>$order_info['shipaddr']['country'],
						'shipstate'=>$order_info['shipaddr']['state'],
						'shippostcode'=>$order_info['shipaddr']['postcode'],
                                                'email'=>$order_info['email'],
						'shipphone'=>$order_info['shipaddr']['phone'],
                                                'billfirstname'=>$order_info['billaddr']['firstname'],
						'billlastname'=>$order_info['billaddr']['lastname'],
						'billaddress'=>$order_info['billaddr']['address'],
						'billcity'=>$order_info['billaddr']['city'],
						'billcountry'=>$order_info['billaddr']['country'],
						'billstate'=>$order_info['billaddr']['state'],
						'billpostcode'=>$order_info['billaddr']['postcode'],
						'billphone'=>$order_info['billaddr']['phone']
						
				);
				
				$this->session->unset_userdata('product');
						
				//$dataaa['posti'] = $this->comm_model->do_pay($send_data);
                                $dataaa['posti'] = $send_data;
                              
                                $this->session->set_userdata('dataaa',$dataaa);
				$this->load->view('posti',$dataaa);
        }

                public function user_mag($action = '',$num = 1){
		
		if($userinfo = $this->session->userdata('userinfo')){
				
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
			
			$uid = $userinfo['mid'];
			
		}else{
			
			header("location:/sign_l");
			
			exit;
			
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		if($all_cart_products = $this->session->userdata('product')){
		
			$data['pro_item'] = count($all_cart_products);
		
		}else{
		
			$data['pro_item'] = 0;
		}
		
		$data['site_info'] = $this->comm_model->get_site_info();
		
		$data['title'] = $data['site_info']['title'];
		$data['keywords'] = $data['site_info']['keyword'];
		$data['description'] = $data['site_info']['discription'];
		
		$site_id = $data['site_info']['siteid'];

		switch($action){
			
			case 'deletewish':
				
				if(!$num) return false;
				
				if($this->comm_model->deletewish($uid,$num)){
					
					header('location:'.$_SERVER['HTTP_REFERER']);
					
					exit;
				}else{
					
					echo "<script>alert('delete failed!');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
					
					exit;
				}
				
				break;
				
			case 'addwishlist':
				
				if($this->comm_model->insert_wish($uid,$num)){
				
					echo "<script>alert('add success!');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
				
					exit;
				
				}else{
				
					echo "<script>alert('add fail!');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
				
					exit;
				}
				
				break;
			
			case 'wishlist':
				
				$product_cart = $this->session->all_userdata();
					
				$data['currency'] = $product_cart['currency'];
					
				$data['symbol'] = $product_cart['currency']->symbol;
				
				$total_page = 0;
					
				$data['product_info'] = $this->comm_model->get_wish($uid,$num,$total_page);
				
				$data['curr_page'] = $num;
				
				$data['total_pages'] = $total_page;
                                
				
				foreach ((array)$data['product_info'] as $key=>$val){
					
					if($key == 'totalpage') unset($data['product_info'][$key]);
				}
				//var_dump($data);
				$this->load->view('header',$data);
				$this->load->view('user_wishlist');
				$this->load_footer();

				break;
			
			case 'orderdetail':
				
				$showproduct = array(
				
						'orderid' => $num
				
				);
				
				$get_key = $this->get_key();
				
				$the_data = $this->encrypt->encode(json_encode($showproduct),$get_key);
				
				$send_info ='the_data='.$the_data .'&siteid='.$site_id;
					
				$res = $this->comm_model->vpost(self::$url."product_list",$send_info);
				
				if($res){
				
					$result = json_decode($res,true);
				
					foreach ($result as $key=>$val){
							
						$re[$key] = $val;
					}

					$ses_info = $this->session->all_userdata();
					
					$currinfo = array();
					
					foreach ($ses_info['order_info']['order'] as $k=>$v){
						
						if($v['orderid'] == $num){
							
							$currinfo = $this->comm_model->get_currency($v['currency']);
						
						}
						
					}
					
					foreach ($re as $k=>$v){
						
						$re[$k]['symbol'] = $currinfo->symbol;
						
						$re[$k]['price'] =$re[$k]['price'];
						
					}
					
					$data['products'] = $re;
					
					foreach ($ses_info['order_info']['order'] as $key=>$val){
						
							if($val['orderid'] == $num){
								
								$order_info = $val;
								
								break;
								
							}
							
					}
						
					$order_info['shipaddr'] = unserialize($order_info['shipaddr']);
					
					$order_info['billaddr'] = unserialize($order_info['billaddr']);
					
					$data['order_info'] = $order_info;
				
				}
				
				$data['countrys'] = $this->comm_model->get_countrys();
				
				$this->load->view('header',$data);
				$this->load->view('user_order_detail');
				$this->load_footer();
					
					
				break;
			
			case 'myorder':
				
				$showorder = array(
				
						'uid' => $uid,
						'index' => $num,
						'pagesize' => '10',
						'siteid' => $site_id
				
				);
				
				$get_key = $this->get_key();
				
				$the_data = $this->encrypt->encode(json_encode($showorder),$get_key);
				
				$send_info ='the_data='.$the_data .'&siteid='.$site_id;
					
				$res = $this->comm_model->vpost(self::$url."order_list",$send_info);

				if($res){
						
					$result = json_decode($res,true);
					
					$this->session->set_userdata('order_info',$result);
					
					foreach ($result['order'] as $k=>$v){
						
						$currinfo = $this->comm_model->get_currency($v['currency']);
						
						$result['order'][$k]['total'] = $currinfo->symbol.ceil($result['order'][$k]['total']*100)/100;
						
					}
	
					$data['order'] = $result['order'];
					
					$data['page'] = $result['page'];
						
				}
				//var_dump($data);
			//	print_r($this->session->userdata('order_info'));exit;
				
				$this->load->view('header',$data);
				$this->load->view('user_order');
				$this->load_footer();
				
				break;
				
			case 'addresslist':
				
				$get_addr = array(
			
							'siteid' => $site_id,
							'uid' => $uid
				
				);
				
				$get_key = $this->get_key();
				
				$the_data = $this->encrypt->encode(json_encode($get_addr),$get_key);
				
				$send_info ='the_data='.$the_data .'&siteid='.$site_id;
					
				$res = $this->comm_model->vpost(self::$url."get_address",$send_info);
				
				if($res){
					
					$result = json_decode($res,true);
					
					foreach ($result as $key=>$val){
							
						$re[$key] = $val;
					}
					
					$data['address']['shipaddr'] = unserialize($re['shipaddr']);
					
					$data['address']['billaddr'] = unserialize($re['billaddr']);
					
				}
				
				$data['countrys'] = $this->comm_model->get_countrys();
				
				$this->load->view('header',$data);
				$this->load->view('user_address');
				$this->load_footer();
				
				break;
				
			case 'account':
				
				$data['userinfo'] = $userinfo;
				
				$this->load->view('header',$data);
				$this->load->view('user_account');
				$this->load_footer();
				
				break;
					
			case 'question':
				
				$get_question = array(

						'uid' => $uid
				
				);
				
				$get_key = $this->get_key();
				
				$the_data = $this->encrypt->encode(json_encode($get_question),$get_key);
				
				$send_info ='the_data='.$the_data .'&siteid='.$site_id;
					
				$res = $this->comm_model->vpost(self::$url."get_question_list",$send_info);
							
				if($res){
						
					$result = json_decode($res,true);
					
					$data['questions'] = $result;
					
				}
				
				$this->load->view('header',$data);
				$this->load->view('user_question');
				$this->load_footer();
			
				break;
				
			default:
				
				$get_addr = array(
			
							'siteid' => $site_id,
							'uid' => $uid
				
				);
				
				$get_key = $this->get_key();
				
				$the_data = $this->encrypt->encode(json_encode($get_addr),$get_key);
				
				$send_info ='the_data='.$the_data .'&siteid='.$site_id;
					
				$res = $this->comm_model->vpost(self::$url."get_address",$send_info);
				
				if($res){
					
					$result = json_decode($res,true);

					
					foreach ($result as $key=>$val){
							
						$re[$key] = $val;
					}
					
					$data['address']['shipaddr'] = unserialize($re['shipaddr']);
					
					$data['address']['billaddr'] = unserialize($re['billaddr']);
					
				}
				
				$data['countrys'] = $this->comm_model->get_countrys();
				
				$this->load->view('header',$data);
				$this->load->view('user_address');
				$this->load_footer();
					
		}
		
		
	}
	
	public function sign_out(){
		
		$this->session->unset_userdata('userinfo');
		
		echo "<script>location.href='/';</script>";
		
		exit;
			
	}
	
	public function addquestion(){
		
		$userinfo = $this->session->userdata('userinfo');
		
		$usinfo = $this->session->userdata('userinfo');
		
		$email = $userinfo['email'];
		
		$site_info = $this->comm_model->get_site_info();
		
		$siteid = $site_info['siteid'];
		
		$post_info = $this->input->post();
		
		$addquestion = array(
				
				'uid' => $userinfo['mid'],
				'question' => $post_info['question'],
				'siteid' => $siteid,
				'email' => $email
		
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($addquestion),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$siteid;
			
		$res = $this->comm_model->vpost(self::$url."add_question/",$send_info);
		
		$result = json_decode($res,true);
		
		if($result['ack'] == -1){
			
			echo "<script>alert('updata fail!');</script>";
		}
		
		echo "<script>location.href='/user_mag/question';</script>";
		
	}
	
	public function addresssave(){
		
		$userinfo = $this->session->userdata('userinfo');
		
		$site_info = $this->comm_model->get_site_info();
		
		$siteid = $site_info['siteid'];
		
		$post_info = $this->input->post();
		
		$shipaddr = array(
					
				'firstname' => 	$post_info['firstname'],
				'lastname' => $post_info['lastname'],
				'address' => $post_info['address'],
				'city' => $post_info['city'],
				'state' => $post_info['state'],
				'postcode' => $post_info['postcode'],
				'country' => $post_info['country'],
				'phone' => $post_info['tel']
					
		);
		$billaddr = array(
					
				'firstname' => 	$post_info['bfirstname'],
				'lastname' => $post_info['blastname'],
				'address' => $post_info['baddress'],
				'city' => $post_info['bcity'],
				'state' => $post_info['bstate'],
				'postcode' => $post_info['bpostcode'],
				'country' => $post_info['bcountry'],
				'phone' => $post_info['btel']
					
		);
		
		$modaddr = array(
					
				'uid' => $userinfo['mid'],
				'siteid' => $siteid,
				'shipaddr' => serialize($shipaddr),
				'billaddr' => serialize($billaddr)
					
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($modaddr),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$siteid;
			
		$res = $this->comm_model->vpost(self::$url."update_address/",$send_info);
		
		$result = json_decode($res,true);
		
		if($result['ack']==0){
			
			echo "<script>location.href='/user_mag';</script>";
			
			exit;
		
		}else{
			
			echo "<script>alert('updata fail!');location.href='/user_mag';</script>";
			
		}
		
		
		
	}
	
	public function get_key(){
		
		$site_info = $this->comm_model->get_site_info();
		
		$site_id = $site_info['siteid'];
		
		$siteid = 'siteid='.$site_id;
		
		return $this->comm_model->vpost(self::$url."site_encrypt",$siteid);

	}
	
	public function updata_uname(){
		
		$userinfo = $this->session->userdata('userinfo');
		
		$post_data = $this->input->post();
		
		$site_info = $this->comm_model->get_site_info();
		
		$siteid = $site_info['siteid'];
		
		$post_info = array(
				
				'uid' => $userinfo['mid'],
				'firstname' => $post_data['firstname'],
				'lastname' => $post_data['lastname']
				
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($post_info),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$siteid;
			
		$res = $this->comm_model->vpost(self::$url."update_name/",$send_info);
		
		$result = json_decode($res,true);
		
		if($result['ack']==0){
			
			foreach ($userinfo as $key=>$val){
				
				if($key == 'firstname'){
					
					$userinfo[$key] = $post_data['firstname'];
					
				}
				
				if($key == 'lastname'){
						
					$userinfo[$key] = $post_data['lastname'];
						
				}
				
			}
			
			$this->session->set_userdata('userinfo',$userinfo);
				
			echo "<script>location.href='/user_mag/account';</script>";
				
			exit;
		
		}else{
				
			echo "<script>alert('updata fail!');location.href='/user_mag/account';</script>";
				
		}
		
	}
	
	public function updata_pword(){
	
		$userinfo = $this->session->userdata('userinfo');
		
		$post_data = $this->input->post();
		
		$site_info = $this->comm_model->get_site_info();
		
		$siteid = $site_info['siteid'];
		
		$post_info = array(
		
				'uid' => $userinfo['mid'],
				'newpassword' => md5($post_data['newpwd']),
				'oldpassword' => md5($post_data['password'])
		
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($post_info),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$siteid;
			
		$res = $this->comm_model->vpost(self::$url."update_password/",$send_info);
		
		$result = json_decode($res,true);
		
		if($result['ack']==0){
		
			echo "<script>alert('update success!');location.href='/user_mag/account';</script>";
		
			exit;
		
		}else{
		
			echo "<script>alert('update fail!');location.href='/user_mag/account';</script>";
		
		}
	
	}
	
        public function return_url(){
            if($userinfo = $this->session->userdata('userinfo')){
				
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
			
			$uid= $userinfo['mid'];
			
			$email = $userinfo['email'];
			
		}
                
              $post_data = $this->input->get();
		
		$OrderId = $post_data['_order_sn'];
                $verified = $post_data["_verified"];
		$Status = $post_data['_verified'] == 'approved'?'paid':$post_data['_verified'];
                
		$Status = $Status == 'test approve'?'test':$Status;
                
                if($Status!='test' && $Status!='paid'){
                    $Status="unpaid";
                }
                
                $site_info = $this->comm_model->get_site_info();
		
		$siteid = $site_info['siteid'];
                
                if($Status == "Success" || $Status=='test'){
                    $post_info = array(
			
					'ordersn' => $OrderId,
					'siteid' => $siteid,
                                        "succ"=>1,
                                        "email"=>$email
			
			);
			$data["msg"]="Your payment has gone through successfully.";
			
                }else{
                     $post_info = array(
			
					'ordersn' => $OrderId,
					'siteid' => $siteid,
                                        "succ"=>0,
                                        "email"=>$email
			
			);
                     $data["msg"]="Pay fail!";
                }
                $get_key = $this->get_key();
			
                $the_data = $this->encrypt->encode(json_encode($post_info),$get_key);
			
		$send_info ='the_data='.$the_data .'&siteid='.$siteid;
				
		$re = $this->comm_model->vpost(self::$url."order_status/",$send_info);
                
                
		$Signature = $post_data['_verifycode'];
		$currency = $post_data['_currency'];
		$amount = $post_data['_total'];
		$transactionid = $post_data['_transactionid'];
		$key = self::$key;
		
		$site_info = $this->comm_model->get_site_info();
		$siteid = $post_data["_siteid"];
		$Signature2 = MD5($OrderId.$siteid.$currency.$amount.$transactionid.$verified.$key);
		
		if($Signature!=$Signature2){
                    echo "签名错误";
                    die();
                }
		
		$site_info = $this->comm_model->get_site_info();
		
		$siteid = $site_info['siteid'];
		
		$post_info = array(
		
				'ordersn' => $OrderId,
				'status' => $Status,
				'transactionid' =>$transactionid
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($post_info),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$siteid;
			
		$ss = $this->comm_model->vpost(self::$url."update_order_status/",$send_info);
		
		//var_dump($ss);
                //echo "Pay success";die(0);
                
		$this->load->view('pay_succ',$data);
		
        }
        
	public function return_page(){
		
		$post_data = $this->input->post();
		
		$OrderId = $post_data['_order_sn'];
		$Status = $post_data['_verified'] == 'approved'?'Success':$post_data['_verified'];
		$Status = $Status == 'test approve'?'test':$Status;
		$Signature = $post_data['_verifycode'];
		$currency = $post_data['_currency'];
		$amount = $post_data['_total'];
		$transactionid = $post_data['_transactionid'];
		$key = self::$key;
		
		$site_info = $this->comm_model->get_site_info();
		$siteid = "487570085";
		
		$Signature2 = MD5($OrderId.$siteid.$currency.$amount.$transactionid.$Status.$key);
		
		if($Signature!=$Signature2){
                    echo "签名错误";
                    die();
                }
		
		$site_info = $this->comm_model->get_site_info();
		
		$siteid = $site_info['siteid'];
		
		$post_info = array(
		
				'orderid' => $OrderId,
				'status' => $Status,
				'transactionid' =>$transactionid
		);
		
		$get_key = $this->get_key();
		
		$the_data = $this->encrypt->encode(json_encode($post_info),$get_key);
		
		$send_info ='the_data='.$the_data .'&siteid='.$siteid;
			
		$ss = $this->comm_model->vpost(self::$url."update_order_status/",$send_info);
		
		//var_dump($ss);
                echo "支付成功";die(0);
		
	}
	
	public function findpwd(){
		
		if($postdata = $this->input->post()){
			
			$site_info = $this->comm_model->get_site_info();
			
			$data['title'] = $site_info['title'];
			$data['keywords'] = $site_info['keyword'];
			$data['description'] = $site_info['discription'];
			
			$siteid = $site_info['siteid'];
			
			$post_info = array(
			
					'email' => $postdata['email'],
					'siteid' => $siteid
			
			);
			
			$get_key = $this->get_key();
			
			$the_data = $this->encrypt->encode(json_encode($post_info),$get_key);
			
			$send_info ='the_data='.$the_data .'&siteid='.$siteid;
				
			$re = $this->comm_model->vpost(self::$url."forget_pwd/",$send_info);
			//var_dump($re);die(0);
			$this->load->view('header',$data);
			$this->load->view('sendpass');
			$this->load_footer();
			
		}else{
			
			$this->load->view('header');
			$this->load->view('findpwd');
			$this->load_footer();
			
		}
			
	}
	
	public function currency($action = ''){
		
		if($action =='icurrency'){
			
			$the_data = json_decode(unserialize($this->input->post('the_data')));
			
			echo $this->comm_model->change_currency($the_data);
			
		}elseif($action){
			
			$currency = $this->comm_model->get_currency($action);
			
			if($currency){
				
				$this->session->set_userdata('currency',$currency);
			}
			
			header("location:".$_SERVER['HTTP_REFERER']);
				
			exit;
			
		}else{
			
			header("location:".$_SERVER['HTTP_REFERER']);
			
			exit;
		}

	}
	
	public function change_token(){
		
		$the_data = json_decode(unserialize($this->input->post('the_data')));
		
		echo $this->comm_model->change_token($the_data->token)?0:-1;
	}
	
	public function contactus(){
		
		if($userinfo = $this->session->userdata('userinfo')){
		
			header("location:/user_mag/question");
			
			exit;
				
		}else{
				
			header("location:/sign_l");
			
			exit;
		}
		
		
	}
	
	public function pro_search(){
		
		$data['products'] = $this->comm_model->pro_search();
		
		if($all_cart_products = $this->session->userdata('product')){
		
			$data['pro_item'] = count($all_cart_products);
		
		}else{
		
			$data['pro_item'] = 0;
		}
		
		$site_info = $this->comm_model->get_site_info();
		
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
		
		$data['categorys'] = $this->comm_model->get_category();
		
		$currency = $this->session->userdata('currency');
		
		foreach ($data['products'] as $key=>$val){
				
			$data['products'][$key]['product_price'] *= $currency->rate;
				
			$data['products'][$key]['product_oprice'] *= $currency->rate;
		}
		
		$data['symbol'] = $currency->symbol;
		
		$data['searchname'] = $this->input->post('pro_name');
		
		$this->load->view('header',$data);
		$this->load->view('search');
		$this->load_footer();
	}
	
	public function cartdelete($product_id = ''){
		
		if($product_id){
			
			$this->comm_model->cartdelete($product_id);
		
		}else {
			
			return false;
		}
		
		header("location:".$_SERVER['HTTP_REFERER']);
		
		exit;
		
	}
	
	public function changeqty(){
		
		$this->comm_model->changeqty();
		
		$ses_info = $this->session->all_userdata();
		
		$total = 0;
		
		foreach ($ses_info['product'] as $k=>$v){
			
			$total += $v['qty']*$v['product_price'];
		}
                $data['currency'] = $ses_info['currency'];
		$data['symbol'] = $ses_info['currency']->symbol;
                
		$data['subtotal'] = ceil($total*100)/100;
		
		if($data['subtotal'] >18000000){
			$this->session->set_userdata('useshipfee',0);
			$data['shippingfee'] = 0;
		}else{
			$this->session->set_userdata('useshipfee',1);
			$data['shippingfee'] = $ses_info['ships']['shippfee']*$ses_info['currency']->rate;
		}
		
		echo json_encode($data);
	}
	
	public function faq(){
		
		if($userinfo = $this->session->userdata('userinfo')){
		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
				
		}
		
		$site_info = $this->comm_model->get_site_info();
		
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
		
		if($all_cart_products = $this->session->userdata('product')){
		
			$data['pro_item'] = count($all_cart_products);
		
		}else{
		
			$data['pro_item'] = 0;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		$this->load->view('header',$data);
		$this->load->view('faq');
		$this->load_footer();
	}
	
	public function imgs($imgurl = '',$imgname = '1',$size = '180.jpg'){
		
		if (!$imgurl||!$imgname){
			
			return false;
		}
		
		if(!isset($_SERVER["HTTP_REFERER"])){
				
			echo "error!";exit;
				
		}
		
		$parse_url=parse_url($_SERVER["HTTP_REFERER"]);

		$url_from=$parse_url['host'];

		if($_SERVER['SERVER_NAME'] == $url_from){
			
			if(strstr($hosturl = $_SERVER['SERVER_NAME'],"www")){
			
				$hosturl = str_replace('www', 'http://image', $hosturl);
			
			}else{
			
				$hosturl = 'http://image.'.$hosturl;
			}
			
			$size = explode(".", $size);
				
			$imgdir = $hosturl.'/img'.$size[0].'/'.$imgurl."/".$imgname.'.'.$size[1];
			
			$img = imagecreatefromjpeg($imgdir);
			
			header('content-type;image/jpeg');
			
			imagejpeg($img);
			
		}else{
			
			return FALSE;
		}
		
	}
	
	public function aboutus(){
		
		if($userinfo = $this->session->userdata('userinfo')){
		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
		
		}
		
		$site_info = $this->comm_model->get_site_info();
		
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
		
		if($all_cart_products = $this->session->userdata('product')){
		
			$data['pro_item'] = count($all_cart_products);
		
		}else{
		
			$data['pro_item'] = 0;
		}
		
		$data['categorys'] = $this->comm_model->get_category();
		
		$this->load->view('header',$data);
		$this->load->view('aboutus');
		$this->load_footer();
		
	}
	
	public function service_shipping(){
		
		if($userinfo = $this->session->userdata('userinfo')){
		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
		
		}
	
		$site_info = $this->comm_model->get_site_info();
	
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
	
		if($all_cart_products = $this->session->userdata('product')){
	
			$data['pro_item'] = count($all_cart_products);
	
		}else{
	
			$data['pro_item'] = 0;
		}
	
		$data['categorys'] = $this->comm_model->get_category();
	
		$this->load->view('header',$data);
		$this->load->view('servershipping');
		$this->load_footer();
	
	}
	
	public function service_return(){
		
		if($userinfo = $this->session->userdata('userinfo')){
		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
		
		}
	
		$site_info = $this->comm_model->get_site_info();
	
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
	
		if($all_cart_products = $this->session->userdata('product')){
	
			$data['pro_item'] = count($all_cart_products);
	
		}else{
	
			$data['pro_item'] = 0;
		}
	
		$data['categorys'] = $this->comm_model->get_category();
	
		$this->load->view('header',$data);
		$this->load->view('return');
		$this->load_footer();
	
	}
	
	public function service_privacy(){
		
		if($userinfo = $this->session->userdata('userinfo')){
		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
		
		}
	
		$site_info = $this->comm_model->get_site_info();
	
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
	
		if($all_cart_products = $this->session->userdata('product')){
	
			$data['pro_item'] = count($all_cart_products);
	
		}else{
	
			$data['pro_item'] = 0;
		}
	
		$data['categorys'] = $this->comm_model->get_category();
	
		$this->load->view('header',$data);
		$this->load->view('privacy');
		$this->load_footer();
	
	}
	
	public function service_terms(){
		
		if($userinfo = $this->session->userdata('userinfo')){
		
			$data['username'] = $userinfo['firstname'].' '.$userinfo['lastname'];
		
		}
	
		$site_info = $this->comm_model->get_site_info();
	
		$data['title'] = $site_info['title'];
		$data['keywords'] = $site_info['keyword'];
		$data['description'] = $site_info['discription'];
	
		if($all_cart_products = $this->session->userdata('product')){
	
			$data['pro_item'] = count($all_cart_products);
	
		}else{
	
			$data['pro_item'] = 0;
		}
	
		$data['categorys'] = $this->comm_model->get_category();
	
		$this->load->view('header',$data);
		$this->load->view('terms');
		$this->load_footer();
	
	}
	
	public function load_footer(){
		
		//var_dump(strstr(uri_string(),'rolex'));exit;
		
		$data['uri'] = strstr(uri_string(),'rolex')||strstr(uri_string(),'Rolex')?'Omega-Constellation-Swiss-ETA-2836-Movement-Full-Rose-Gold-with-White-Dial':uri_string();
			
		$this->load->view('footer',$data);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

